﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using ChapterX.BusinessFacade;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.DigitizationDSTableAdapters;

namespace ChapterX.Modules
{
    public partial class ApplicationListModule : ChapterX.Model.DataListModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int digStatus = -1;
                if (Utils.HasValue(Request.QueryString[QueryKeys.DIGISTATUS_ID]))
                {

                    try
                    {
                        digStatus = Int32.Parse(Utils.GetValue(Request.QueryString[QueryKeys.DIGISTATUS_ID]));
                    }
                    catch (Exception ex)
                    {
                    }
                }
                this.hf_DigiStatus.Value = digStatus.ToString();
                BindApplicationList(digStatus);
            }
        }

        protected void BindApplicationList(int digStatus)
        {
            try
            {
                DigitizationViewTableAdapter adapter = new DigitizationViewTableAdapter();
                DataTable appList = null;

                if (CurrentUserInfo.IsLibrarian)
                {
                    if (digStatus < 0)
                        appList = adapter.GetData();
                    else
                        appList = adapter.GetDigitizationByStatusFromView(digStatus);
                }
                else if (CurrentUserInfo.IsTeacher)
                {
                    if (digStatus < 0)
                        appList = adapter.GetDigitizationByTeacherFromView(CurrentUserInfo.Username);
                    else
                        appList = adapter.GetDigitizationByTeacherAndStatusFromView(CurrentUserInfo.Username, digStatus);
                }
                else
                {
                    throw new UnexpectedException();
                }

                ApplicationList.DataSource = appList;
                ApplicationList.DataBind();
            }
            catch (Exception ex)
            {
                SetErrorMessage("数据绑定失败。");
                Logger.Log(ex);
            }
        }

        protected void ApplicationList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Button b = e.Item.FindControl("button_Operation") as Button;
            HiddenField hf_DigStatus = e.Item.FindControl("hf_DigStatus") as HiddenField;
            HiddenField hf_BookId = e.Item.FindControl("hf_BookId") as HiddenField;

            if (b != null && hf_DigStatus != null && hf_BookId != null)
            {
                try
                {
                    int digStatus = Int32.Parse(hf_DigStatus.Value);
                    if (digStatus == (int)CommonDefinition.DigitizationStatus.defaultStatus)
                    {
                        b.Text = CommonDefinition.OperationText_Default;
                        b.OnClientClick = "javascript:return confirm('您确定要提交数字化申请吗？')";
                    }
                    else if (digStatus == (int)CommonDefinition.DigitizationStatus.submitted)
                    {
                        if (CurrentUserInfo.IsTeacher)
                        {
                            b.Text = CommonDefinition.OperationText_Submitted;
                            b.Enabled = false;
                        }
                        else
                        {
                            b.Text = CommonDefinition.OperationText_Submitted_Librarian;
                            b.Enabled = true;
                        }
                    }
                    else if (digStatus == (int)CommonDefinition.DigitizationStatus.digitized)
                        b.Text = CommonDefinition.OperationText_Digitized;
                    else if (digStatus == (int)CommonDefinition.DigitizationStatus.denied)
                    {
                        b.Text = CommonDefinition.OperationText_Denied;
                        b.OnClientClick = "javascript:return confirm('您确定要重新提交数字化申请吗？')";
                    }


                    b.CommandArgument = hf_BookId.Value;
                    b.CommandName = b.Text;
                }
                catch{}
            }
        }

        protected void ApplicationList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                int bookId = Int32.Parse(e.CommandArgument.ToString());

                if (e.CommandName == CommonDefinition.OperationText_Default)
                {
                    //提交申请，修改状态为已提交申请

                    bool result = false;
                    DigitizationSystem ds = new DigitizationSystem();
                    result = ds.UpdateDigitizationStatus(bookId, 
                       (int)CommonDefinition.DigitizationStatus.submitted);

                    if (result)
                    {
                        SetInfoMessage("数字化申请提交成功");
                        
                        //刷新状态？？？？？
                    }
                    else
                        SetErrorMessage("数字化申请提交失败");
                }
                else if (e.CommandName == CommonDefinition.OperationText_Submitted)
                {
                    Response.Redirect(String.Format("ApplicationDetail.aspx?{0}={1}", QueryKeys.BOOK_ID, bookId.ToString()));
                }
                else if (e.CommandName == CommonDefinition.OperationText_Submitted_Librarian)
                {
                    Response.Redirect(String.Format("ApplicationDetail.aspx?{0}={1}", QueryKeys.BOOK_ID, bookId.ToString()));
                }
                else if (e.CommandName == CommonDefinition.OperationText_Digitized)
                {
                    Response.Redirect(String.Format("ApplicationDetail.aspx?{0}={1}", QueryKeys.BOOK_ID, bookId.ToString()));
                }
                else if (e.CommandName == CommonDefinition.OperationText_Denied)
                {
                    //提交申请，修改状态为已提交申请

                    bool result = false;
                    DigitizationSystem ds = new DigitizationSystem();
                    result = ds.UpdateDigitizationStatus(bookId,
                       (int)CommonDefinition.DigitizationStatus.submitted);

                    if (result)
                    {
                        SetInfoMessage("数字化申请提交成功");

                        //刷新状态？？？？？
                    }
                    else
                        SetErrorMessage("数字化申请提交失败");
                }
            }
            catch { }
        }

    }
}